package com.timoon.sample.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.timoon.component.panel.jqgrid.JqGridHelper;
import com.timoon.component.panel.jqgrid.JqGridParameter;
import com.timoon.component.panel.jqgrid.JqGridService;
import com.timoon.sample.dao.ProductCommentDAO;
import com.timoon.sample.dao.ProductDAO;
import com.timoon.sample.dao.ProductInstructionPicDAO;
import com.timoon.sample.dao.ProductPropertyDAO;
import com.timoon.sample.dao.model.Product;
import com.timoon.sample.dao.model.ProductComment;
import com.timoon.sample.dao.model.ProductCommentExample;
import com.timoon.sample.dao.model.ProductExample;
import com.timoon.sample.dao.model.ProductInstructionPic;
import com.timoon.sample.dao.model.ProductInstructionPicExample;
import com.timoon.sample.dao.model.ProductProperty;
import com.timoon.sample.dao.model.ProductPropertyExample;
import com.timoon.webcomponent.utility.product.linepic.HorizontalList4PicProductPanelService;
import com.timoon.webcomponent.utility.product.single.SingleProductService;
import com.timoon.webcomponent.utility.product.single.component.ProductCommentListService;
import com.timoon.webcomponent.utility.product.single.component.ProductDetailTabService;

/**
 *<p></p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */

@Service("com.timoon.webcomponent.utility.product.service.ProductService")
public class ProductServiceImpl implements SingleProductService,HorizontalList4PicProductPanelService
					,ProductDetailTabService,ProductCommentListService,JqGridService{
	
	private Logger logger=LoggerFactory.getLogger(this.getClass());

	@Resource
	private ProductDAO productDAOImpl;
	
	@Resource
	private ProductPropertyDAO productPropertyDAOImpl;
	
	@Resource
	private ProductCommentDAO productCommentDAOImpl;
	
	@Resource
	private ProductInstructionPicDAO productInstructionPicDAOImpl;
	
	public ProductInstructionPicDAO getProductInstructionPicDAOImpl() {
		return productInstructionPicDAOImpl;
	}

	public void setProductInstructionPicDAOImpl(
			ProductInstructionPicDAO productInstructionPicDAOImpl) {
		this.productInstructionPicDAOImpl = productInstructionPicDAOImpl;
	}

	@Override
	public Product loadProductById(Integer id) {
		// TODO Auto-generated method stub
		return this.productDAOImpl.selectByPrimaryKey(id);
	}

	public ProductPropertyDAO getProductPropertyDAOImpl() {
		return productPropertyDAOImpl;
	}

	public void setProductPropertyDAOImpl(ProductPropertyDAO productPropertyDAOImpl) {
		this.productPropertyDAOImpl = productPropertyDAOImpl;
	}

	public ProductDAO getProductDAOImpl() {
		return productDAOImpl;
	}

	public ProductCommentDAO getProductCommentDAOImpl() {
		return productCommentDAOImpl;
	}

	public void setProductCommentDAOImpl(ProductCommentDAO productCommentDAOImpl) {
		this.productCommentDAOImpl = productCommentDAOImpl;
	}

	public void setProductDAOImpl(ProductDAO productDAOImpl) {
		this.productDAOImpl = productDAOImpl;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Product> getProductByType(final Integer type) {
		// TODO Auto-generated method stub
		return this.productDAOImpl.selectByExample(new ProductExample(){{
			this.createCriteria().andStatusNotEqualTo(0).andTypeEqualTo(type);
		}});
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProductProperty> getProductPropertyListByProductId(final Integer id) {
		// TODO Auto-generated method stub
		return this.productPropertyDAOImpl.selectByExample(new ProductPropertyExample(){{
			this.createCriteria().andStatusNotEqualTo(0).andProductIdEqualTo(id);
		}});
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProductComment> getAllCommentByProductId(final Integer id) {
		// TODO Auto-generated method stub
		return this.productCommentDAOImpl.selectByExample(new ProductCommentExample(){{
			this.createCriteria().andPublicedNotEqualTo(0).andProductIdEqualTo(id);
		}});
	}

	@Override
	public Product getProductById(Integer id) {
		// TODO Auto-generated method stub
		return this.productDAOImpl.selectByPrimaryKey(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProductInstructionPic> getProductInstructionPicsByProductId(
			final Integer id) {
		// TODO Auto-generated method stub
		return this.productInstructionPicDAOImpl.selectByExample(new ProductInstructionPicExample(){{
			this.createCriteria().andProductIdEqualTo(id);
		}});
	}

	@Override
	public String getGridData(HttpServletRequest request) {
		// TODO Auto-generated method stub
		
		JqGridParameter param=new JqGridParameter();
		JqGridHelper helper=new JqGridHelper();
		
		Map<String,String> paramMap=helper.coverParameterMap(request);
		param.setMap(paramMap);
		
		Integer records=this.productDAOImpl.countProductForPaging(param);
		Integer rows=Integer.valueOf(paramMap.get("rows").toString());
		Integer page=Integer.valueOf(paramMap.get("page").toString());
		Integer startIndex=helper.getStartIndex(rows,page,records);
		
		param.setPage(page);
		param.setRows(rows);
		param.setStartIndex(startIndex);
		
		List<Map<String,Object>> maps=this.productDAOImpl.paginProductToMap(param);
		
		String json=helper.getJqJSON(maps);
		
		this.logger.debug(json);
		return json;
	}

	@Override
	public Integer countGridData(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deleteByIds(String deleteIds) {
		// TODO Auto-generated method stub
		if(deleteIds!=null){
			String[] ids=deleteIds.split(",");
			for(String id:ids){
				Product product=this.productDAOImpl.selectByPrimaryKey(Integer.valueOf(id));
				
				product.setStatus(0);
				this.productDAOImpl.updateByPrimaryKey(product);
			}
		}
	}

	@Override
	public void saveOrUpdateRecord(Map<String, Object> map) {
		// TODO Auto-generated method stub
		
		Product product=(Product) map.get("product");
		
		if(product.getId()==null){
			this.productDAOImpl.insert(product);
		}else{
			this.productDAOImpl.updateByPrimaryKey(product);
		}
	}

	@Override
	public Object loadRecordById(String id) {
		// TODO Auto-generated method stub
		return this.productDAOImpl.selectByPrimaryKey(Integer.valueOf(id));
	}


	
}
